<?php

namespace App\Http\Controllers\System;

use App\Library\api_sdk\SmsDemo;
use App\System\ErrorModel;
use App\User\UserBaseModel;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Cache;
use App\Library\sms\top\request\AlibabaAliqinFcSmsNumSendRequest;
use App\Library\sms\top\TopClient;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class CoreController extends Controller
{
    public function reg_mail()
    {
        //
    }

    /**
     * @param string        $token      存储的token key
     * @param UserBaseModel $user_info  存储的用户信息
     * @返回 bool
     */
    public static function set_user_login_status(string $token,$user_info) : bool
    {

        $cacheKey       = CacheKeyController::$USER_LOGIN_STATUS;
        $userCacheKey   = $cacheKey['key'] . $token;

        if (!is_array($user_info)){
            $user_info = $user_info->toArray();
        }

        if ($user_info)
        {
            Cache::store('redis')->put($userCacheKey,$user_info,$cacheKey['time']);
            Cache::store('redis')->put($user_info['id'],$userCacheKey,$cacheKey['time']);

        }
        return true;
    }

    /**
     *  发送库存报警短信
     */
    public static function send_alert_sms($phone='',$item,$num)
    {
        $tempArr    = "SMS_146290955";
        $demo = new SmsDemo();
        $response = $demo->sendSms(
            '次元局', // 短信签名
            $tempArr, // 短信模板编号
            $phone, // 短信接收者
            [
                'item'  => $item,
                'num'   => $num
            ],
            "123"
        );
        return ['phone'=>$phone,'res'=>$response->Code];
    }
    public static function send_sms($phone,$msg,$tmp,$sign='次元局')
    {
        $demo = new SmsDemo();

        $response = $demo->sendSms(
            $sign, // 短信签名
            $tmp, // 短信模板编号
            $phone, // 短信接收者
            $msg //模板值
        );
        $res = ['phone'=>$phone,'res'=>$response->Code];

        return $res;
    }
    public static function sendCode($phone=17602114904,$msg='',$tempId=0,$sign='次元局',$smsDemo = SmsDemo::class)
    {
        // 0信息变更验证码 1修改密码验证码 2用户注册验证码  3登录异常验证码  4登录确认验证码  5短信测试   6身份验证验证码  7.SMS_112465127 提现
        $tempArr    = ["SMS_129800010"];

        $demo = new $smsDemo();

        $code = (string)rand(1000, 9999);
        $end_time   = time() + 15*60;
        Redis::HMSET('user_sms_'.$phone, ['code'=>$code , 'end_time'=> $end_time]);
        Redis::EXPIRE ('user_sms_'.$phone,15*60);
        session(['user_sms_'.$phone => ['code'=>$code , 'end_time'=>time() + 15*60 ]]);
        $response = $demo->sendSms(
            $sign, // 短信签名
            $tempArr[$tempId], // 短信模板编号
            $phone, // 短信接收者
            Array(  // 短信模板中字段的值
                "code"      => $code
            ),
            "123"
        );

        $errCode = [
            'isp.RAM_PERMISSION_DENY'                                                   =>  'RAM权限DENY',
            'isv.OUT_OF_SERVICE'                                                                  =>  '业务停机',
            'isv.PRODUCT_UN_SUBSCRIPT'                                                  =>  '未开通云通信产品的阿里云客户',
            'isv.PRODUCT_UNSUBSCRIBE'                                                   =>  '产品未开通',
            'isv.ACCOUNT_NOT_EXISTS'                                                       =>  '账户不存在',
            'isv.ACCOUNT_ABNORMAL'                                                       =>  '账户异常',
            'isv.SMS_TEMPLATE_ILLEGAL'                                                    =>  '短信模板不合法',
            'isv.SMS_SIGNATURE_ILLEGAL'                                                  =>  '短信签名不合法',
            'isv.INVALID_PARAMETERS'                                                       =>  '参数异常',
            'isp.SYSTEM_ERROR'                                                                     =>  '系统错误',
            'isv.MOBILE_NUMBER_ILLEGAL'                                                =>  '非法手机号',
            'isv.MOBILE_COUNT_OVER_LIMIT'                                            =>  '手机号码数量超过限制',
            'isv.TEMPLATE_MISSING_PARAMETERS'                                 =>  '模板缺少变量',
            'isv.BUSINESS_LIMIT_CONTROL'                                                =>  '业务限流',
            'isv.INVALID_JSON_PARAM'                                                       =>  'JSON参数不合法，只接受字符串值',
            'isv.BLACK_KEY_CONTROL_LIMIT'                                              =>  '黑名单管控',
            'isv.PARAM_LENGTH_LIMIT'                                                        =>  '参数超出长度限制',
            'isv.PARAM_NOT_SUPPORT_URL'                                              =>  '不支持URL',
            'isv.AMOUNT_NOT_ENOUGH'                                                    =>  '账户余额不足',
        ];
        if (isset ( $errCode [$response->Code] )) {
//            ErrorModel::save_error($errCode [$response->Code],'send_sms');
            $ret =  $errCode [$response->Code]??$response->Code;
        }
        $res = ['code'=>$code,'phone'=>$phone,'res'=>$response->Code,'code'=>$code];
//        $res = ['code'=>$code,'phone'=>$phone,'res'=>'ok','code'=>$code];

        return $res;
    }

    public static function check_sms_code(string $phone,string $code) : bool
    {
        $user_session = Redis::HGETALL("user_sms_".$phone);

        if (empty($user_session)) {
            return false;
        }

        if ($user_session['end_time'] < time()) {
            return false;
        }

        if ($user_session['code'] != $code) {
            return false;
        }
        return true;
    }

}
